﻿using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using OPCPlatformConfigure.BllBusiness.OPCServer;
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static OPCPlatformConfigure.OracleHelper;

namespace OPCPlatformConfigure
{
    public partial class frmOPCServerConfi : Form
    {
        string mainId;
        DataTable dtTemp;
        bllOPCServer bll;
        public frmOPCServerConfi(DataRow dataTemp)
        {
            InitializeComponent();
            bll = new bllOPCServer();
            if (dataTemp == null)
            {
                txtMachineName.Properties.ReadOnly = false;
                txtMachineName.Text = "";
                dtTemp = bll.GetTags("-1");
                btnDelete.Enabled = false;
            }
            else
            {
                txtMachineName.Properties.ReadOnly = true;
                txtMachineName.Text = dataTemp["MACHINENAME"].ToString();
                mainId = dataTemp["GUID"].ToString();
                dtTemp = bll.GetTags(mainId);
                btnDelete.Enabled = true;
            }
            gcTagInfos.DataSource = dtTemp;
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            IWorkbook workBook = new XSSFWorkbook(@"Data\OPCServerData.xlsx");
            ISheet sheet = workBook.GetSheetAt(0);
            dtTemp.Rows.Clear();
            for (int i = 1; i <= sheet.LastRowNum; i++)
            {
                IRow row = sheet.GetRow(i);
                DataRow rowTemp = dtTemp.NewRow();
                rowTemp["FRIENDLYNAME"] = row.Cells[0].StringCellValue;
                rowTemp["ADDRESS"] = row.Cells[1].StringCellValue;
                rowTemp["NEEDREAD"] = row.Cells[2].BooleanCellValue;
                rowTemp["NEEDWRITE"] = row.Cells[3].BooleanCellValue;
                dtTemp.Rows.Add(rowTemp);
            }
            gcTagInfos.Refresh();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (dtTemp.Rows.Count <= 0)
            {
                MessageBox.Show("保存前请先导入数据!");
                return;
            }
            if (string.IsNullOrEmpty(txtMachineName.Text.Trim()))
            {
                MessageBox.Show("机台名称不能为空!");
                return;
            }
            List<OracleParameterWrapper> oracleParameters = new List<OracleParameterWrapper>();
            oracleParameters.Add(new OracleParameterWrapper("FRIENDLYNAME", OracleDbType.Varchar2));
            oracleParameters.Add(new OracleParameterWrapper("ADDRESS", OracleDbType.Varchar2));
            oracleParameters.Add(new OracleParameterWrapper("NEEDREAD", OracleDbType.Varchar2));
            oracleParameters.Add(new OracleParameterWrapper("NEEDWRITE", OracleDbType.Varchar2));

            for (int i = 0; i < dtTemp.Rows.Count; i++)
            {
                oracleParameters[0].ParameterValues.Add(dtTemp.Rows[i]["FRIENDLYNAME"].ToString());
                oracleParameters[1].ParameterValues.Add(dtTemp.Rows[i]["ADDRESS"].ToString());
                oracleParameters[2].ParameterValues.Add(dtTemp.Rows[i]["NEEDREAD"].ToString());
                oracleParameters[3].ParameterValues.Add(dtTemp.Rows[i]["NEEDWRITE"].ToString());
            }
            bll.InsertMahine(oracleParameters, txtMachineName.Text.Trim(), mainId);
            MessageBox.Show("保存成功!");
            this.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (mainId != null&&MessageBox.Show("您确定需要删除当前机台配置吗?","警告",MessageBoxButtons.OKCancel)==DialogResult.OK)
            {
                bll.DeleteTagsById(mainId, true);
                MessageBox.Show("删除成功!");
                this.Close();
            }
        }
    }
}
